package org.sacgtug.android;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class JsonDbAdapter {

	private static final String TAG = "JsonDbAdapter";
	
	private DatabaseHelper mDatabaseHelper;
    private SQLiteDatabase mSQLiteDatabase;
    private static final String DATABASE_NAME = "data";
    private static final String DATABASE_TABLE = "kv";
    private static final int DATABASE_VERSION = 3;
    
    public static final String FIELD_ID = "_id";
    public static final String FIELD_JSON = "json";
    
    private static final String[] FIELDS = new String[]{ FIELD_ID, FIELD_JSON };
    
    private static final String SELECT_BY_ID = FIELD_ID + "=";
    private static final String WHERE_ID = FIELD_ID + "=";
    
    private static final String CREATE_DATABASE =
        "create table " + DATABASE_TABLE + " (" + FIELD_ID + " integer primary key autoincrement, " + FIELD_JSON + " text not null);";
    
    private final Context mContext;
    
    public JsonDbAdapter(Context context) {
    	
    	mContext = context;
    }
    
    public JsonDbAdapter open() throws SQLException {
    	
    	mDatabaseHelper = new DatabaseHelper(mContext, DATABASE_NAME, null, DATABASE_VERSION);
    	mSQLiteDatabase = mDatabaseHelper.getWritableDatabase();
    	return this;
    }
    
    public void close() {
    	
    	mDatabaseHelper.close();
    }

    // insert
    public long put(String json) {
    	
    	ContentValues contentValues = new ContentValues();
    	contentValues.put(FIELD_JSON, json);
    	return mSQLiteDatabase.insert(DATABASE_TABLE, null, contentValues);
    }
    
    // delete
    public boolean delete(long rowId) {
    	
    	return mSQLiteDatabase.delete(DATABASE_TABLE, SELECT_BY_ID + rowId, null) > 0;
    }
    
    // select all
    public Cursor get() {
    	
    	return mSQLiteDatabase.query(DATABASE_TABLE, FIELDS, null, null, null, null, null);
    }
    
    // select one
    public Cursor get(long rowId) throws SQLException {
    	
    	Cursor cursor = mSQLiteDatabase.query(true, DATABASE_TABLE, FIELDS, SELECT_BY_ID + rowId, null, null, null, null, null);
    	
    	if(null != cursor) {
    		cursor.moveToFirst();
    	}
    	
    	return cursor;
    }
    
    // update
    public boolean put(long rowId, String json) {
    	
    	ContentValues contentValues = new ContentValues();
    	contentValues.put(FIELD_JSON, json);
    	
    	return mSQLiteDatabase.update(DATABASE_TABLE, contentValues, WHERE_ID + rowId, null) > 0;
    }
     
    private static class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context, String name, CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			
			db.execSQL(CREATE_DATABASE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase arg0, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from verison " + oldVersion + " to " + newVersion + ", which will destroy all existing data");
			
		}
    }
}
